package com.aw.guiguzi.widgets

import android.app.Activity
import android.graphics.drawable.BitmapDrawable
import android.os.Build
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.Spinner
import android.widget.TextView
import com.aw.baselib.base.AwBaseConstant
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.util.AwLog
import com.aw.baselib.util.AwPopupwindowUtil
import com.aw.baselib.util.AwScreenUtils
import com.aw.baselib.util.AwToastUtil
import com.aw.guiguzi.R
import com.aw.guiguzi.base.MyApp
import com.aw.guiguzi.bean.NumImgItemBean
import com.aw.guiguzi.bean.NumItemBean
import com.aw.guiguzi.enums.NumBettingEnums
import com.aw.guiguzi.util.AppCacheUtil
import com.aw.guiguzi.util.CalResultUtil.generateNumList
import com.aw.guiguzi.util.CalResultUtil.getDefaultConfigBlockIndex
import com.aw.guiguzi.util.CalResultUtil.getSpecialBlockList
import java.util.Arrays

class NumImgPopupWindow(
    private val mActivity: Activity,
    callback: OnResultCallback?,
    dismissListener: OnDismissListener?
) : PopupWindow() {
    private val contentView: View?
    private var mList: List<NumItemBean?> = ArrayList()
    private val ll_doubleConfig: LinearLayout
    private var ll_0_1234: LinearLayout? = null
    private var ll_56789: LinearLayout? = null
    private var ll_0: LinearLayout? = null
    private var ll_1: LinearLayout? = null
    private var ll_2: LinearLayout? = null
    private var ll_3: LinearLayout? = null
    private var ll_4: LinearLayout? = null
    private var ll_5: LinearLayout? = null
    private var ll_6: LinearLayout? = null
    private var ll_7: LinearLayout? = null
    private var ll_8: LinearLayout? = null
    private var ll_9: LinearLayout? = null
    private var iv_0_1234: ImageView? = null
    private var iv_56789: ImageView? = null
    private var iv_0: ImageView? = null
    private var iv_1: ImageView? = null
    private var iv_2: ImageView? = null
    private var iv_3: ImageView? = null
    private var iv_4: ImageView? = null
    private var iv_5: ImageView? = null
    private var iv_6: ImageView? = null
    private var iv_7: ImageView? = null
    private var iv_8: ImageView? = null
    private var iv_9: ImageView? = null
    private var iv_0_1: ImageView? = null
    private var iv_0_2: ImageView? = null
    private var iv_13: ImageView? = null
    private var iv_23: ImageView? = null
    private var iv_24: ImageView? = null
    private var iv_35: ImageView? = null
    private var iv_45: ImageView? = null
    private var iv_46: ImageView? = null
    private var iv_57: ImageView? = null
    private var iv_67: ImageView? = null
    private var iv_68: ImageView? = null
    private var iv_79: ImageView? = null
    private var iv_89: ImageView? = null
    private var iv_0_123: ImageView? = null
    private var iv_2345: ImageView? = null
    private var iv_4567: ImageView? = null
    private var iv_6789: ImageView? = null
    private var mReConfirmDialog: BettingReConfirmDialog? = null
    private val mImgList: MutableList<NumImgItemBean> = ArrayList()

    init {
        MyApp.mAllBettingNumsList = generateNumList()
        mList = MyApp.mAllBettingNumsList
        contentView = View.inflate(mActivity, R.layout.view_popupwindow_num_img, null)
        initView()
        ll_doubleConfig = contentView.findViewById(R.id.ll_doubleConfig)
        val cBox_double5 = contentView.findViewById<ImageView>(R.id.cBox_double5)
        val cBox_double2 = contentView.findViewById<ImageView>(R.id.cBox_double2)
        val cBox_double2Half5 = contentView.findViewById<ImageView>(R.id.cBox_double2Half5)
        val iv_close = contentView.findViewById<ImageView>(R.id.iv_close)
        val tv_submit = contentView.findViewById<TextView>(R.id.tv_submit)
        val tv_config = contentView.findViewById<Spinner>(R.id.tv_config)
        val tv_block = contentView.findViewById<Spinner>(R.id.tv_block)
        setCheckBoxView(cBox_double5, AppCacheUtil.isBettingConfigDouble5())
        setCheckBoxView(cBox_double2, AppCacheUtil.isBettingConfigDouble2())
        setCheckBoxView(cBox_double2Half5, AppCacheUtil.getBettingConfigDouble2Half5())
        iv_close.setOnClickListener { v: View? -> dismiss() }
        tv_submit.setOnClickListener { v: View? ->
            if (null != callback) {
                val selectList: MutableList<NumItemBean?> = ArrayList()
                for (itemBean in mList) {
                    if (itemBean!!.isChecked) {
                        selectList.add(itemBean)
                    }
                }
                if (AwDataUtil.isEmpty(selectList)) {
                    AwToastUtil.showShort(mActivity, "请至少选择一项数据")
                    return@setOnClickListener
                }
                if (AppCacheUtil.isBettingReConfirm()) {
                    //需再次确认, 弹框展示
                    mReConfirmDialog = BettingReConfirmDialog()
                    mReConfirmDialog?.showDialog(
                        mActivity,
                        selectList,
                        View.OnClickListener { v1: View? -> callback.onResult(selectList, "") })
                } else {
                    callback.onResult(selectList, "")
                }
            }
            dismiss()
        }
        tv_config.setSelection(defaultConfigCountIndex)
        tv_config.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>,
                view: View,
                position: Int,
                id: Long
            ) {
                setListItemCount(parent.selectedItem as String)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {}
        }
        if (AppCacheUtil.isBettingDefault()) {
            tv_block.setSelection(getDefaultConfigBlockIndex(mActivity))
        } else {
            tv_block.setSelection(0)
            clearAllSelect()
        }
        tv_block.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>,
                view: View,
                position: Int,
                id: Long
            ) {
                setSpecialBlockView(parent.selectedItem as String)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {}
        }
        contentView.findViewById<View>(R.id.iv_delConfigNums)
            .setOnClickListener { v: View? -> clearAllSelect() }
        cBox_double5.setOnClickListener { v: View? ->
            AppCacheUtil.setBettingConfigDouble5(!AppCacheUtil.isBettingConfigDouble5())
            setCheckBoxView(cBox_double5, AppCacheUtil.isBettingConfigDouble5())
            for (itemBean in mList) {
                if (itemBean!!.is5()) {
                    itemBean.setDouble(AppCacheUtil.isBettingConfigDouble5())
                }
            }
        }
        cBox_double2.setOnClickListener { v: View? ->
            AppCacheUtil.setBettingConfigDouble2(!AppCacheUtil.isBettingConfigDouble2())
            setCheckBoxView(cBox_double2, AppCacheUtil.isBettingConfigDouble2())
            for (itemBean in mList) {
                if (itemBean!!.is2()) {
                    itemBean.setDouble(AppCacheUtil.isBettingConfigDouble2())
                }
            }
        }
        cBox_double2Half5.setOnClickListener { v: View? ->
            AppCacheUtil.setBettingConfigDouble2Half5(!AppCacheUtil.getBettingConfigDouble2Half5())
            setCheckBoxView(cBox_double2Half5, AppCacheUtil.getBettingConfigDouble2Half5())
            for (itemBean in mList) {
                if (itemBean!!.is2Point5()) {
                    itemBean.setDouble(AppCacheUtil.getBettingConfigDouble2Half5())
                }
            }
        }
        setContentView(contentView)
        this.width = ViewGroup.LayoutParams.MATCH_PARENT
        this.height = (AwScreenUtils.getScreenHeight(mActivity) * 0.8).toInt()
        this.animationStyle = com.aw.baselib.R.style.sytle_anim_pop_in_out
        this.isFocusable = true
        this.isTouchable = true
        this.isOutsideTouchable = true
        setBackgroundDrawable(BitmapDrawable())
        this.update()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.overlapAnchor = true
        }
        if (dismissListener == null) {
            setOnDismissListener {
                val lp = mActivity.window.attributes
                lp.alpha = AwBaseConstant.Popupwindow.POPUPWINDOW_NORMAL
                mActivity.window.attributes = lp
            }
        } else {
            setOnDismissListener(dismissListener)
        }
    }

    private fun setCheckBoxView(iv: ImageView, isChecked: Boolean) {
        if (isChecked) {
            iv.background = mActivity.resources.getDrawable(R.drawable.icon_checkbox_on)
        } else {
            iv.background = mActivity.resources.getDrawable(R.drawable.icon_checkbox_off)
        }
    }

    private fun clearAllSelect() {
        for (itemBean in mList) {
            itemBean!!.isChecked = false
        }
        for (imgItemBean in mImgList) {
            imgItemBean.isChecked = false
            imgItemBean.iv?.isSelected = false
        }
    }

    private fun initView() {
        if (contentView == null) {
            return
        }
        //        if(AwDataUtil.isEmpty(MyApp.mAllBettingNumImgList)) {
        ll_0_1234 = contentView.findViewById(R.id.ll_0_1234)
        ll_56789 = contentView.findViewById(R.id.ll_56789)
        ll_0 = contentView.findViewById(R.id.ll_0)
        ll_1 = contentView.findViewById(R.id.ll_1)
        ll_2 = contentView.findViewById(R.id.ll_2)
        ll_3 = contentView.findViewById(R.id.ll_3)
        ll_4 = contentView.findViewById(R.id.ll_4)
        ll_5 = contentView.findViewById(R.id.ll_5)
        ll_6 = contentView.findViewById(R.id.ll_6)
        ll_7 = contentView.findViewById(R.id.ll_7)
        ll_8 = contentView.findViewById(R.id.ll_8)
        ll_9 = contentView.findViewById(R.id.ll_9)
        iv_0_1234 = contentView.findViewById(R.id.iv_0_1234)
        iv_56789 = contentView.findViewById(R.id.iv_56789)
        iv_0 = contentView.findViewById(R.id.iv_0)
        iv_1 = contentView.findViewById(R.id.iv_1)
        iv_2 = contentView.findViewById(R.id.iv_2)
        iv_3 = contentView.findViewById(R.id.iv_3)
        iv_4 = contentView.findViewById(R.id.iv_4)
        iv_5 = contentView.findViewById(R.id.iv_5)
        iv_6 = contentView.findViewById(R.id.iv_6)
        iv_7 = contentView.findViewById(R.id.iv_7)
        iv_8 = contentView.findViewById(R.id.iv_8)
        iv_9 = contentView.findViewById(R.id.iv_9)
        iv_0_1 = contentView.findViewById(R.id.iv_0_1)
        iv_0_2 = contentView.findViewById(R.id.iv_0_2)
        iv_13 = contentView.findViewById(R.id.iv_13)
        iv_23 = contentView.findViewById(R.id.iv_23)
        iv_24 = contentView.findViewById(R.id.iv_24)
        iv_35 = contentView.findViewById(R.id.iv_35)
        iv_45 = contentView.findViewById(R.id.iv_45)
        iv_46 = contentView.findViewById(R.id.iv_46)
        iv_57 = contentView.findViewById(R.id.iv_57)
        iv_67 = contentView.findViewById(R.id.iv_67)
        iv_68 = contentView.findViewById(R.id.iv_68)
        iv_79 = contentView.findViewById(R.id.iv_79)
        iv_89 = contentView.findViewById(R.id.iv_89)
        iv_0_123 = contentView.findViewById(R.id.iv_0_123)
        iv_2345 = contentView.findViewById(R.id.iv_2345)
        iv_4567 = contentView.findViewById(R.id.iv_4567)
        iv_6789 = contentView.findViewById(R.id.iv_6789)
        mImgList.add(NumImgItemBean(ll_0_1234, iv_0_1234, "01234"))
        mImgList.add(NumImgItemBean(ll_56789, iv_56789, "56789"))
        mImgList.add(NumImgItemBean(ll_0, iv_0, "0"))
        mImgList.add(NumImgItemBean(ll_1, iv_1, "1"))
        mImgList.add(NumImgItemBean(ll_2, iv_2, "2"))
        mImgList.add(NumImgItemBean(ll_3, iv_3, "3"))
        mImgList.add(NumImgItemBean(ll_4, iv_4, "4"))
        mImgList.add(NumImgItemBean(ll_5, iv_5, "5"))
        mImgList.add(NumImgItemBean(ll_6, iv_6, "6"))
        mImgList.add(NumImgItemBean(ll_7, iv_7, "7"))
        mImgList.add(NumImgItemBean(ll_8, iv_8, "8"))
        mImgList.add(NumImgItemBean(ll_9, iv_9, "9"))
        mImgList.add(NumImgItemBean(null, iv_0_1, "01"))
        mImgList.add(NumImgItemBean(null, iv_0_2, "02"))
        mImgList.add(NumImgItemBean(null, iv_13, "13"))
        mImgList.add(NumImgItemBean(null, iv_23, "23"))
        mImgList.add(NumImgItemBean(null, iv_24, "24"))
        mImgList.add(NumImgItemBean(null, iv_35, "35"))
        mImgList.add(NumImgItemBean(null, iv_45, "45"))
        mImgList.add(NumImgItemBean(null, iv_46, "46"))
        mImgList.add(NumImgItemBean(null, iv_57, "57"))
        mImgList.add(NumImgItemBean(null, iv_67, "67"))
        mImgList.add(NumImgItemBean(null, iv_68, "68"))
        mImgList.add(NumImgItemBean(null, iv_79, "79"))
        mImgList.add(NumImgItemBean(null, iv_89, "89"))
        mImgList.add(NumImgItemBean(null, iv_0_123, "0123"))
        mImgList.add(NumImgItemBean(null, iv_2345, "2345"))
        mImgList.add(NumImgItemBean(null, iv_4567, "4567"))
        mImgList.add(NumImgItemBean(null, iv_6789, "6789"))

//            MyApp.mAllBettingNumImgList = mImgList;
//        } else {
//            mImgList = MyApp.mAllBettingNumImgList;
//        }
        for (itemBean in mImgList) {
            setNumListener(itemBean.ll_layout, itemBean.iv!!, itemBean.nameTag)
        }
        for (itemBean in mList) {
            if (itemBean!!.isChecked) {
                for (imgItemBean in mImgList) {
                    if (imgItemBean.nameTag == itemBean.name) {
                        AwLog.d("isChecked && equals: " + imgItemBean.nameTag)
                        imgItemBean.isChecked = true
                        imgItemBean.iv?.isSelected = true
                    }
                }
            }
        }
    }

    private fun setNumListener(ll_layout: LinearLayout?, iv: ImageView, numTag: String) {
        if (ll_layout != null) {
            ll_layout.setOnClickListener(View.OnClickListener { v: View? ->
                iv.isSelected = !iv.isSelected
                for (itemBean in mList) {
                    if (itemBean!!.name == numTag) {
                        itemBean.isChecked = iv.isSelected
                    }
                }
            })
        } else {
            iv.setOnClickListener { v: View? ->
                iv.isSelected = !iv.isSelected
                for (itemBean in mList) {
                    if (itemBean!!.name == numTag) {
                        itemBean.isChecked = iv.isSelected
                    }
                }
            }
        }
    }

    private val defaultConfigCountIndex: Int
        private get() {
            val list = Arrays.asList(*mActivity.resources.getStringArray(R.array.config_count))
            return list.indexOf(AppCacheUtil.getBettingConfigCount())
        }

    private fun setListItemCount(name: String) {
        if (AwDataUtil.isEmpty(mList)) {
            return
        }
        AppCacheUtil.setBettingConfigCount(name)
        for (itemBean in mList) {
            if (name == NumBettingEnums.MODE_1_1_10.name) {
                itemBean!!.setSilverCount(1)
                itemBean.setSkinCount(1)
                itemBean.setGemstoneCount(10)
            } else if (name == NumBettingEnums.MODE_1_1_20.name) {
                itemBean!!.setSilverCount(1)
                itemBean.setSkinCount(1)
                itemBean.setGemstoneCount(20)
            } else if (name == NumBettingEnums.MODE_1_1_40.name) {
                itemBean!!.setSilverCount(1)
                itemBean.setSkinCount(1)
                itemBean.setGemstoneCount(40)
            } else if (name == NumBettingEnums.MODE_2_2_20.name) {
                itemBean!!.setSilverCount(2)
                itemBean.setSkinCount(2)
                itemBean.setGemstoneCount(20)
            } else if (name == NumBettingEnums.MODE_2_2_40.name) {
                itemBean!!.setSilverCount(2)
                itemBean.setSkinCount(2)
                itemBean.setGemstoneCount(40)
            } else if (name == NumBettingEnums.MODE_3_3_30.name) {
                itemBean!!.setSilverCount(3)
                itemBean.setSkinCount(3)
                itemBean.setGemstoneCount(30)
            } else if (name == NumBettingEnums.MODE_4_4_40.name) {
                itemBean!!.setSilverCount(4)
                itemBean.setSkinCount(4)
                itemBean.setGemstoneCount(40)
            } else if (name == NumBettingEnums.MODE_5_5_50.name) {
                itemBean!!.setSilverCount(5)
                itemBean.setSkinCount(5)
                itemBean.setGemstoneCount(50)
            } else if (name == NumBettingEnums.MODE_5_5_100.name) {
                itemBean!!.setSilverCount(5)
                itemBean.setSkinCount(5)
                itemBean.setGemstoneCount(100)
            } else if (name == NumBettingEnums.MODE_10_10_100.name) {
                itemBean!!.setSilverCount(10)
                itemBean.setSkinCount(10)
                itemBean.setGemstoneCount(100)
            } else if (name == NumBettingEnums.MODE_10_10_200.name) {
                itemBean!!.setSilverCount(10)
                itemBean.setSkinCount(10)
                itemBean.setGemstoneCount(200)
            } else {
                val bettingCustomArrays = AppCacheUtil.getBettingCustom().split("/".toRegex())
                    .dropLastWhile { it.isEmpty() }
                    .toTypedArray()
                itemBean!!.setSilverCount(AwDataUtil.getIntValue(bettingCustomArrays[0]))
                itemBean.setSkinCount(AwDataUtil.getIntValue(bettingCustomArrays[1]))
                itemBean.setGemstoneCount(AwDataUtil.getIntValue(bettingCustomArrays[2]))
            }
        }
    }

    private fun setSpecialBlockView(block: String) {
        clearAllSelect()
        val list = getSpecialBlockList(block)
        for (imgItemBean in mImgList) {
            if (list.contains(imgItemBean.nameTag)) {
                imgItemBean.iv?.isSelected = true
                for (itemBean in mList) {
                    if (itemBean!!.name == imgItemBean.nameTag) {
                        itemBean.isChecked = true
                    }
                }
            }
        }
    }

    /**
     * 展示窗口
     */
    fun show(parent: View?) {
        val lp = mActivity.window.attributes
        lp.alpha = AwBaseConstant.Popupwindow.POPUPWINDOW_DARK
        mActivity.window.attributes = lp
        this.showAsDropDown(parent, 0, 0)
    }

    fun showAsLocation(parent: View?) {
        val lp = mActivity.window.attributes
        lp.alpha = AwBaseConstant.Popupwindow.POPUPWINDOW_DARK
        mActivity.window.attributes = lp
        val windowPos = AwPopupwindowUtil.calculatePopWindowPos(parent, contentView)
        val xOff = 20 // 可以自己调整偏移
        windowPos[0] -= xOff
        showAtLocation(parent, Gravity.TOP or Gravity.START, windowPos[0], windowPos[1])
    }

    fun showAsLocationNoAlpha(parent: View?) {
        val windowPos = AwPopupwindowUtil.calculatePopWindowPos(parent, contentView)
        val xOff = 20 // 可以自己调整偏移
        windowPos[0] -= xOff
        showAtLocation(parent, Gravity.TOP or Gravity.START, windowPos[0], windowPos[1])
    }

    fun showAsLocationBottom(parent: View?) {
        val lp = mActivity.window.attributes
        lp.alpha = AwBaseConstant.Popupwindow.POPUPWINDOW_DARK
        mActivity.window.attributes = lp
        showAtLocation(parent, Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, 0, 0)
    }

    /**
     * calendar菜单选择监听接口
     */
    interface OnResultCallback {
        fun onResult(list: List<NumItemBean?>?, desc: String?)
    }
}